package com.smart.test;

import com.smart.entity.*;
import com.smart.service.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootTest
public class testForCookies {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IViolationRecordsService violationRecordsService;
    @Autowired
    private IEnvironmentDataService environmentDataService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private ISensorService sensorService;

    @Test
    public void updateEmployeeUid() {
        List<Employee> employeeList = employeeService.lambdaQuery().list();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (Employee employee : employeeList) {
            LocalDate entryDate = employee.getEntryDate();
            String joinDate = entryDate.format(formatter);
            System.err.println(joinDate);
            // String[] joinDateArray = joinDate.split(" ");
            String[] dateArray = joinDate.split("-");
            String[] yearArray = dateArray[0].split("0");
            String datePart = yearArray[1] + dateArray[1] + dateArray[2];
            Integer count = employeeService.lambdaQuery()
                    .likeRight(Employee::getUid, datePart)
                    .count();
            count += 1;
            String countString = count < 10 ? "0" + count : count.toString();
            String uid = datePart + countString;
            employee.setUid(uid);
            System.err.println(uid);
            // employeeService.updateById(employee);
        }
    }

    @Test
    public void updateDeviceToMakeSureEveryDeviceTypeBeUsed() {
        List<DeviceType> deviceTypeList = deviceTypeService.lambdaQuery().list();
        int size = deviceTypeList.size();
        Random random = new Random();
        while (true) {
            for (int i = 1; i <= 66; i++) {
                int num = random.nextInt(size);
                Device device = deviceService.lambdaQuery().eq(Device::getId, i).one();
                DeviceType deviceType = deviceTypeList.get(num);
                device.setType(deviceType.getType());
                device.setSubType(deviceType.getSubType());
                device.setName(deviceType.getSubTypeName());
                deviceService.updateById(device);
            }
            List<Device> deviceList = deviceService.lambdaQuery().list();
            Set<String> uniqueSubTypes = deviceList.stream().map(Device::getSubType).collect(Collectors.toSet());
            if (uniqueSubTypes.size() == 20) {
                break;
            }
        }
    }

    @Test
    public void addViolationData() {
        List<Employee> employeeList = employeeService.lambdaQuery().list();
        Map<String, String> violationData = new HashMap<>();
        Random random = new Random();
        violationData.put("安全帽未佩戴", "工人未佩戴安全帽");
        violationData.put("危险区域闯入", "工人违规进入危险区域");
        violationData.put("安全绳未系", "工人在高空作业时未系安全绳");
        violationData.put("机械设备操作不当", "工人操作起重机不符合安全规范");
        violationData.put("无防护服操作", "工人在化学品作业区域未穿戴防护服");
        violationData.put("违规停放车辆", "车辆违规停放阻碍了施工区域");
        violationData.put("工地未清理", "工地材料堆放混乱，未及时清理");
        for (Employee employee : employeeList) {
            ViolationRecords violationRecords = new ViolationRecords();
            int num = random.nextInt(violationData.size());
            String key = (String) violationData.keySet().toArray()[num];
            String value = violationData.get(key);
            violationRecords.setId(employee.getId());
            violationRecords.setViolationType(key);
            violationRecords.setDescription(value);
            violationRecords.setViolatorUid(employee.getUid());
            violationRecords.setViolationLocation("工地" + random.nextInt(10) + "区");
            // System.out.println(violationRecords);
            violationRecordsService.updateById(violationRecords);
        }
    }

    @Test
    public void addEnvironmentData() {
        Random random = new Random();
        List<Sensor> sensorList = sensorService.list();
        LocalDate localDate = LocalDate.now();
        List<LocalTime> localTimeList = Arrays.asList(
                LocalTime.of(0, 0, 0),
                LocalTime.of(8, 0, 0),
                LocalTime.of(12, 0, 0),
                LocalTime.of(16, 0, 0),
                LocalTime.of(20, 0, 0)
        );
        for (int i = 6; i >= 0; i--) {
            for (int j = 0; j < 5; j++) {
                for (int k = 0; k < 24; k++) {
                    Sensor sensor = sensorList.get(k);
                    LocalDateTime date = LocalDateTime.of(localDate, localTimeList.get(j)).minusDays(i);
                    EnvironmentData environmentData = new EnvironmentData();
                    environmentData.setDeviceId(sensor.getId().toString());
                    environmentData.setDeviceLocation(sensor.getLocation());
                    environmentData.setType(sensor.getType());
                    environmentData.setDeviceTime(date);
                    environmentData.setDoserate(BigDecimal.valueOf(random.nextInt(60) + 20));
                    environmentDataService.save(environmentData);
                }
            }
        }
    }
}